package logic

import (
	"common"
	"fmt"
)

var trieTree *Trie = NewTrie()

//将数据加入到Trie树中
func InitGoods(data []common.Goods) (err error) {
	var Goods_name common.RespData
	for _, value := range data {
		//将商品名字加入trie树
		//每个节点的node中的data保存完整的数据，通过Trie树找到节点,之后提取到里面的信息
		Goods_name.Goods_name = value.Goods_name
		trieTree.Add(value.Goods_name, Goods_name)
	}

	return
}

//Trie树:前缀搜索
func PrefixSearch(keyword string, limit int) (data []interface{}) {
	nodes := trieTree.PrefixSearch(keyword, limit)

	//fmt.Printf("len:%d\n", len(nodes))

	for _, v := range nodes {
		//遍历nodes,nodes中全是查找到的完整的字符串的node,之后提取每一个node中的商品信息
		data = append(data, v.Data)
	}

	return
}

//Trie树:敏感词搜索
func Search(keyword string) bool {
	nodes := trieTree.Search(keyword)
	fmt.Print(nodes)
	return nodes
}
